// API endpoints
const API = {
    SET_DIR: '/api/models/dir',
    LIST_MODELS: '/api/models/list',
    SELECT_MODEL: '/api/model/select',
    START_SYNTHESIS: '/api/synthesis/start',
    STOP_SYNTHESIS: '/api/synthesis/stop'
};

// DOM Elements
const elements = {
    modelDir: document.getElementById('modelDir'),
    modelList: document.getElementById('modelList'),
    text: document.getElementById('text')
};

// Set models directory
async function setModelDir() {
    const dir = elements.modelDir.value.trim();
    if (!dir) {
        alert('请输入模型目录路径');
        return;
    }

    try {
        const response = await fetch(API.SET_DIR, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ dir })
        });

        const data = await response.json();
        if (response.ok) {
            alert('模型目录设置成功');
            refreshModelList();
        } else {
            alert(`错误: ${data.error}`);
        }
    } catch (error) {
        alert(`请求失败: ${error.message}`);
    }
}

// Refresh model list
async function refreshModelList() {
    try {
        const response = await fetch(API.LIST_MODELS);
        const data = await response.json();
        
        if (response.ok) {
            // Clear existing options
            elements.modelList.innerHTML = '<option value="">请选择模型</option>';
            
            // Add new options
            data.models.forEach(model => {
                const option = document.createElement('option');
                option.value = model.name;
                option.textContent = model.name;
                elements.modelList.appendChild(option);
            });

            // Select current model if exists
            if (data.current_model) {
                elements.modelList.value = data.current_model;
            }
        } else {
            alert(`获取模型列表失败: ${data.error}`);
        }
    } catch (error) {
        alert(`请求失败: ${error.message}`);
    }
}

// Select model
async function selectModel(modelName) {
    if (!modelName) return;

    try {
        const response = await fetch(API.SELECT_MODEL, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ model: modelName })
        });

        const data = await response.json();
        if (response.ok) {
            alert(`模型 "${modelName}" 选择成功`);
        } else {
            alert(`错误: ${data.error}`);
        }
    } catch (error) {
        alert(`请求失败: ${error.message}`);
    }
}

// Start synthesis
async function startSynthesis() {
    const text = elements.text.value.trim();
    if (!text) {
        alert('请输入要合成的文本');
        return;
    }

    try {
        const response = await fetch(API.START_SYNTHESIS, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ text })
        });

        const data = await response.json();
        if (response.ok) {
            alert('开始合成');
        } else {
            alert(`错误: ${data.error}`);
        }
    } catch (error) {
        alert(`请求失败: ${error.message}`);
    }
}

// Stop synthesis
async function stopSynthesis() {
    try {
        const response = await fetch(API.STOP_SYNTHESIS, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            }
        });

        const data = await response.json();
        if (response.ok) {
            alert(data.message);
        } else {
            alert(`错误: ${data.error}`);
        }
    } catch (error) {
        alert(`请求失败: ${error.message}`);
    }
}

// Initialize
document.addEventListener('DOMContentLoaded', () => {
    refreshModelList();
});
